<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>

        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{ this.$store.state.city }}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="item in hot"
            :key="item.id"
            @click="handelClickCity(item.name)"
          >
            <div class="button">{{ item.name }}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{ key }}</div>

        <div class="item-list">
          <div
            class="item border-bottom"
            v-for="innerItem in item"
            :key="innerItem.id"
            @click="handelClickCity(innerItem.name)"
          >
            {{ innerItem.name }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from "better-scroll";
import { mapMutations } from "vuex";
export default {
  name: "CityList",
  props: {
    hot: Array,
    cities: Object,
    letter: String,
  },
  methods: {
    handelClickCity(city) {
      this.changeCity(city);
      this.$router.push("/");
    },
    ...mapMutations(["changeCity"]),
  },

  watch: {
    letter() {
      if (this.letter) {
        const element = this.$refs[this.letter][0];

        this.scroll.scrollToElement(element);
      }
    },
  },
  mounted() {
    this.scroll = new Bscroll(this.$refs.wrapper, { click: true });
  },
};
</script>

<style lang="stylus" scoped>
.list {
  position: absolute;
  overflow: hidden;
  top: 2.89rem;
  left: 0;
  right: 0;
  bottom: 0;
}

.title {
  line-height: 1.5rem;
  color: #666;
  background: #eee;
  font-size: 0.44rem;
}

.button-list {
  overflow: hidden;
  padding: 0.3rem;
}

.button-wrapper {
  float: left;
  width: 30.33%;
}

.button {
  margin: 0.1rem;
  text-align: center;
  border: 0.02rem solid #ccc;
  line-height: 0.8rem;
}

.item-list {
  padding-left: 0.4rem;
  line-height: 1.2rem;
}
</style>